package com.dyh.quickdemo.bizDemo.Eigenvector;

import java.util.*;
import java.util.function.Consumer;

/*
@Author=Joey Wheeler
@Date=2021/2/24 13:09
*/
public class T1 {

    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        for (int i=0;i<10000;i++){
            Random random = new Random();
            Student student = new Student(
                    random.nextInt(24)+7,
                    random.nextInt(2)<1?'M':'F'
            );
            students.add(student);
        }

        Map<String, Consumer<Student>> executors =
                new HashMap<>();
        executors.put("M1",e-> System.out.println("参军"));
        executors.put("M0",e-> System.out.println("后备军"));

        Date start1 = new Date();
        for (Student student : students) {
            if (student.getAge()<10)
                System.out.println("回去");
            else if (student.getSex()=='F')
                System.out.println("回去");
            else if(student.getAge()<16)
                System.out.println("后备军");
            else
                System.out.println("参军");
        }
        Date end1 = new Date();
        System.out.println("ifelse1耗时"+(end1.getTime()-start1.getTime()));

        Date start2 = new Date();
        for (Student student : students) {
//            Optional.ofNullable(executors.get(student.normalize()))
//                    .orElse(e-> System.out.println("回去"))
//                    .accept(student);
               Consumer<Student>customer = executors.get(student.normalize());
                if (customer==null){
                    System.out.println("回去");
                }
                else {
                    customer.accept(student);
                }
        }
        Date end2 = new Date();
        System.out.println("优化后耗时"+(end2.getTime()-start2.getTime()));


        Date start3 = new Date();
        for (Student student : students) {
            if (student.getAge()<10)
                System.out.println("回去");
            else if (student.getSex()=='F')
                System.out.println("回去");
            else if(student.getAge()<16)
                System.out.println("后备军");
            else
                System.out.println("参军");
        }
        Date end3 = new Date();
        System.out.println("ifelse2耗时"+(end3.getTime()-start3.getTime()));







    }
}
